//
//  EGOCustomImageView.m
//  DemoMeshtiles
//
//  Created by MAC on 7/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "EGOCustomImageView.h"

@implementation EGOCustomImageView
@synthesize hud=_hud;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithPlaceholderImage:(UIImage *)anImage {
    self=[super initWithPlaceholderImage:anImage];
    if(self) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(showMainMenu:) 
                                                     name:@"abc" object:nil];
        
        
        
    }
    return self;
}
-(void)setImageURL:(NSURL *)aURL {
    if(super.imageURL) {
		[[EGOImageLoader sharedImageLoader] removeObserver:self forURL:super.imageURL];
		[super.imageURL release];
		super.imageURL = nil;
	}
	
	if(!aURL) {
		self.image = self.placeholderImage;
		super.imageURL = nil;
		return;
	} else {
		super.imageURL = [aURL retain];
	}
    
	[[EGOImageLoader sharedImageLoader] removeObserver:self];
	UIImage* anImage = [[EGOImageLoader sharedImageLoader] imageForURL:aURL shouldLoadWithObserver:self];
	
	if(anImage) {
		self.image = anImage;
        
		// trigger the delegate callback if the image was found in the cache
		if([self.delegate respondsToSelector:@selector(imageViewLoadedImage:)]) {
			[self.delegate imageViewLoadedImage:self];
		}
	} else {
		self.image = self.placeholderImage;
        if (self.hud==nil) {
            _hud = [[MBProgressHUD alloc] initWithView:self];
            self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        } 
        self.hud.progress=0;
        self.hud.hidden=NO;
        self.hud.mode = MBProgressHUDModeAnnularDeterminate;
	}

    
}
- (void)showMainMenu:(NSNotification *)note {
    
    if ([note.name  isEqualToString:@"abc"]) {
        EGOImageLoadConnection *connection=(EGOImageLoadConnection*)note.object;
        if (connection.isLoadImagePhoto==NO) {
            NSLog(@"Percent %f",connection.currLength*1.0/connection.contentLength);
            _hud.progress=connection.currLength*1.0/connection.contentLength;
        } else {
            NSLog(@"Finish");
            _hud.hidden=YES;
        }
    }
    
}


@end
